#!/usr/bin/perl -w # for syntax highlighting
package clreqd_modules;

#########################################################################
# This rountine takes the details of an event monitor and loads them	#
# into the local configuration database if they are successfully	#
# validated.								#
#									#
# Arguments are:							#
# appname, script							#
#									#
# script - the hex-encoded script.					#
# appname - the name of the application [if type is 'app'].		#
#									#
# Returns:								#
# OK - record added/replaced as requested.				#
# ERROR msg - an error has occured - see 'msg'.				#
#########################################################################

use cldb;
use Data::Dumper;
use File::Temp qw(tempfile);

sub set_custom_probe_info {
my %ARGS=@_;
my $f;

	if(! grep {$_ eq "$::ENV{TRUECL_CODE}/monitors"} @::INC) {
		push @::INC,"$::ENV{TRUECL_CODE}/monitors";
	}
	my $appname=$ARGS{APPNAME} if exists($ARGS{APPNAME});
	my $script=pack("h*",$ARGS{SCRIPT});

	#################################################################
	# Before we load it into the environment we run it with the	#
	# arguments APPname validate and it must return OK.		#
	#################################################################

	my ($fdd,$filename)=tempfile();
	if(! print $fdd $script) {
		close($fdd); unlink($filename);
		return "ERROR Unable to write to temporary file.";
	}
	close($fdd);
	chmod(0700,$filename);
	my $r=`$filename $appname validate 2>&1`;
	chomp $r;
	if($r ne "OK") {
		unlink($filename);
		return "ERROR Script validation failed:\n$r";
	}
	unlink($filename);

	#################################################################
	# So everything is OK - now we just need to write out the 	#
	# new data into the correct table. This is straightforward.	#
	#################################################################

	if(! -f "$::ENV{TRUECL_CFG}/config.db") {
		return("ERROR Failed to find database '$::ENV{TRUECL_CFG}/config.db'!");
	}

	#################################################################
	# Database does exist, so open it up ready for changing.	#
	#################################################################

	my $db=new cldb("$::ENV{TRUECL_CFG}/config.db");
	if(!defined($db)) {
		return("ERROR Unable to open database 'config.db':\n" . cldb::error);
	}
	::msg("Opened database connection to '$::ENV{TRUECL_CFG}/config.db'.");

	#################################################################
	# Ensure the table exists and abort if not.			#
	#################################################################

	if(! $db->exists("table","custom_app_probes")) {
		return("ERROR Config database missing 'custom_app_probes' table.");
	}
	my $s="SELECT COUNT(*) as count FROM custom_app_probes WHERE name = '$appname' " if defined($appname);
	$s.=";";
	my $res=$db->get_rows($s);
	if(!defined($res) || !defined($res->[0]->{count})) {
		my $e=cldb::error;
		if(defined($e)) {
			return "ERROR $e";
		} else {
			return "ERROR No count was returned for existing custom application probes!";
		}
	}

	if($res->[0]->{count}) {
		$s="DELETE FROM custom_app_probes WHERE name = '$appname';";
		$r=$db->do($s);
		if(!$r) {
			my $e=cldb::error;
			if(defined($e)) {
				return "ERROR Unable to remove record from database. $e";
			} else {
				return "ERROR Unable to remove record from database.";
			}
		}
	}

	#################################################################
	# Any existing record has been deleted - so new record		#
	# can be added.							#
	#################################################################
	my $t=time;
	$s="INSERT INTO custom_app_probes (name,script,update_time) VALUES ('$appname','$ARGS{SCRIPT}','$t');";
	$r=$db->do($s);
	if(!$r) {
		my $e=cldb::error;
		if(defined($e)) {
			return "ERROR Unable to add record to database. $e";
		} else {
			return "ERROR Unable to add record to database.";
		}
	}
	$db->disconnect; $db=undef;
	return "OK";
}

1;
